<?php
session_start();
include_once "conexion.php";

function verificar_login($user,$password,&$result) {
    $sql = "SELECT * FROM Usuarios WHERE email = '$user' and clave = '$password'";
    $rec = mysql_query($sql);
    $count = 0;

    while($row = mysql_fetch_object($rec)){
        $count++;
        $result = $row;
    }

    if($count == 1){
        return 1;
    }else{
        return 0;
    }
}


if(!isset($_SESSION['userid'])){
    if(isset($_POST['login'])){
        if(verificar_login($_POST['user'],$_POST['password'],$result) == 1){
            $origen= $_SERVER["HTTP_REFERER"];
            header('location:'.$origen);
            $_SESSION['userid'] = $result->cedula;
            $_SESSION['nomuser'] = $result->nombre;
            $_SESSION['apeuser'] = $result->apellido;
        }else{
            echo '<div class="error">Usuario o password, intente nuevamente.</div>';
            header('location:'.$origen);
        }
    }
    
?>

<style type="text/css">
*{
	font-size: 14px;
}
form.login {
    background: none repeat scroll 0 0 #F1F1F1;
    border: 1px solid #DDDDDD;
    font-family: sans-serif;
    margin: 0 auto;
    padding: 20px;
    width: 278px;
}
form.login div {
    margin-bottom: 15px;
    overflow: hidden;
}
form.login div label {
    display: block;
    float: left;
    line-height: 25px;
}
form.login div input[type="text"], form.login div input[type="password"] {
    border: 1px solid #DCDCDC;
    float: right;
    padding: 4px;
}
form.login div input[type="submit"] {
    background: none repeat scroll 0 0 #DEDEDE;
    border: 1px solid #C6C6C6;
    float: right;
    font-weight: bold;
    padding: 4px 20px;
}
.error{
    color: red;
    font-weight: bold;
    margin: 10px;
    text-align: center;
}
</style>



<form action="" method="post" class="login">
	<div><label>Correo</label><input name="user" type="text" ></div>
	<div><label>Password</label><input name="password" type="password"></div>
	<div><input name="login" type="submit" value="acceder"></div>
</form>
<?php
} else {
  $nm =	$_SESSION['nomuser'];
  $ap = $_SESSION['apeuser'];
	echo '<br><span style="color: #000;text-shadow: #DADADA 1px 1px 1px;font-size: 0.9em;margin-left:150px;">'.$nm.' '.$ap.'</span> ';
	echo '<a href="logout.php" style="text-decoration:none;background-color:#DADADA;color:#000;padding:1px 8px 1px 8px;">Salir</a>';
}


?>


